package com.jh3502.helper.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.Gson;
import com.jh3502.helper.Jh3502HelperApplication;
import com.jh3502.helper.function.person.personlist.PersonListBean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SpHelper {

    private static final String TAG = "SpHelper";
    private static final String PROJECT_NAME = "3502_helper";
    private static final String TOKEN = "token";
    private static final String ID = "id";
    private static final String USERNAME = "user_name";
    private static final String PASSWORD = "password";
    private static final String NAME = "name";
    private static final String METHOD = "method";
    private static final String PHONE = "phone";
    private static final String ROLES = "roles";
    private static final String ICON = "icon";
    private static final String PERSON_SP = "person_sp";
    private static final String LAST_VERSION_CHECK_TIME = "last_version_check_time";
    private static final String SPLASH_AD_URL = "splash_ad_url";
    private static final String SPLASH_CONTENT_URL = "splash_content_url";
    private static final String BANNER_URL = "banner_url";
    private static final String HOME_ICON = "home_icon";
    private static final String BATCH_LIST = "ba";
    private static final String LIST_RULE_MALE = "list_rule_male";
    private static final String LIST_RULE_FEMALE = "list_rule_female";
    private static final String LT_SHAPE_RULE_MALE = "lt_shape_rule_male";
    private static final String LT_SHAPE_RULE_FEMALE = "lt_shape_rule_female";
    private static final String TEXT_SIZE = "text_size";
    private static final String BAT_ACT = "batno_act";

    private static SpHelper spHelper;
    @NonNull
    public Set<String> homeIconSet = new HashSet<>();
    private SharedPreferences mSharedPreferences;

    private SpHelper() {
        this(Jh3502HelperApplication.getAppContext(), Context.MODE_PRIVATE);
    }

    private SpHelper(Context context, int mode) {


        mSharedPreferences = context.getSharedPreferences(PROJECT_NAME, mode);

        homeIconSet.add("0");
    }

    public static synchronized SpHelper getInstance() {
        if (spHelper == null) {
            spHelper = new SpHelper();
        }
        return spHelper;
    }


    public boolean putString(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public boolean removeString(String key) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove(key);
        return editor.commit();
    }

    @Nullable
    public String getString(String key) {
        return mSharedPreferences.getString(key, "");
    }

    public boolean putInt(String key, int value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public boolean putStringSet(String key, Set<String> value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putStringSet(key, value);
        return editor.commit();
    }

    @Nullable
    public Set<String> getStringSet(String key) {
        return mSharedPreferences.getStringSet(key, null);
    }


    public boolean putHome(Set<String> value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putStringSet(HOME_ICON, value);
        return editor.commit();
    }

    @Nullable
    public Set<String> getHome() {
        return mSharedPreferences.getStringSet(HOME_ICON, homeIconSet);
    }


    public int getInt(String key) {
        return mSharedPreferences.getInt(key, -1);
    }

    public boolean putLong(String key, long value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public long getLong(String key) {
        return mSharedPreferences.getLong(key, -1);
    }

    @Nullable
    public String getToken() {
        return getString(TOKEN);
    }

    public boolean setUserName(String value) {
        return putString(USERNAME, value);
    }

    public boolean setPassword(String value) {
        return putString(PASSWORD, value);
    }

    public boolean setToken(String value) {
        return putString(TOKEN, value);
    }

    public boolean setRoles(String value) {
        return putString(ROLES, value);
    }


    public void saveAll(@NonNull Map<String, Object> map) {
        putString(TOKEN, (String) map.get("token"));
        putString(NAME, Utils.getInstance().getMapString(map, "name"));
        putString(METHOD, Utils.getInstance().getMapString(map, "method"));
        putString(PHONE, Utils.getInstance().getMapString(map, "phone"));
        putString(ID, Utils.getInstance().getMapString(map, "id"));
    }

    public void clearAll() {
        putString(TOKEN, "");
        putString(NAME, "");
        putString(METHOD, "");
        putString(PHONE, "");
        putString(ID, "");
        putString(USERNAME, "");
        putString(PASSWORD, "");


    }

    public boolean setBatchList(ArrayList<Map<String, Object>> datalist) {
        return putString(BATCH_LIST, new Gson().toJson(datalist));
    }

    public boolean setBatchList(String datalist) {
        return putString(BATCH_LIST, datalist);
    }

    @NonNull
    public ArrayList<Map<String, Object>> getBatchList() {
        return Utils.getInstance().jsa2list(getString(BATCH_LIST));
    }


    public boolean setSp(String datalist) {
        return putString(PERSON_SP, datalist);
    }

    @Nullable
    public Map<String, Object> getSp() {
        return Utils.getInstance().jso2map(getString(PERSON_SP));
    }

    @Nullable
    public String getListltruleFemale() {
        return getString(LIST_RULE_FEMALE);
    }

    public boolean setListltruleFemale(String datalist) {
        return putString(LIST_RULE_FEMALE, datalist);
    }

    @Nullable
    public String getListltruleMale() {
        return getString(LIST_RULE_MALE);
    }

    public boolean setListltruleMale(String datalist) {
        return putString(LIST_RULE_MALE, datalist);
    }

    @Nullable
    public String getLtShapeRuleFemale() {
        return getString(LT_SHAPE_RULE_FEMALE);
    }

    public boolean setLtShapeRuleFemale(String datalist) {
        return putString(LT_SHAPE_RULE_FEMALE, datalist);
    }

    @Nullable
    public String getLtShapeRuleMale() {
        return getString(LT_SHAPE_RULE_MALE);
    }

    public boolean setLtShapeRuleMale(String datalist) {
        return putString(LT_SHAPE_RULE_MALE, datalist);
    }

    public boolean setPersonList(String batchName, Map<String, Object> map) {
        return putString(batchName, new Gson().toJson(map));
    }

    public boolean setPersonList(String batchName, @NonNull List<PersonListBean> list) {
        return putString(batchName, new Gson().toJson(Utils.getInstance().person2Map(list)));
    }

    public boolean setPersonList(@NonNull Map<String, Object> map) {
        return putString((String) map.get("batno"), (String) map.get("datalist"));
    }

    public boolean setPersonListBean(String batchName, List<PersonListBean> datalist) {
        return putString(batchName, new Gson().toJson(datalist));
    }


    @NonNull
    public List<PersonListBean> getPersonList(String batch_name) {
        List<PersonListBean> arrayList = Utils.getInstance().map2Person(getString(batch_name));
//        ArrayList<PersonListBean> arrayList =
//                new Gson().fromJson(getString(batch_name), new TypeToken<List<PersonListBean>>() {
//                }.getType());
        return arrayList;
    }

    @Nullable
    public String getUserName() {
        return getString(USERNAME);
    }

    @Nullable
    public String getPassword() {
        return getString(PASSWORD);
    }


    @Nullable
    public String getRoles() {
        return getString(ROLES);
    }

    @Nullable
    public String getPhone() {
        return getString(PHONE);
    }

    public long getLastVersionCheckTime() {
        return getLong(LAST_VERSION_CHECK_TIME);
    }

    public void setLastVersionCheckTime(long time) {
        putLong(LAST_VERSION_CHECK_TIME, time);
    }


    public boolean setSplashAdUrl(String splashAdUrl) {
        return putString(SPLASH_AD_URL, splashAdUrl);
    }

    @Nullable
    public String getSplashAdUrl() {
        return getString(SPLASH_AD_URL);
    }


    public boolean setTextSize(int textSize) {
        return putInt(TEXT_SIZE, textSize);
    }

    public int getTextSize() {
        return getInt(TEXT_SIZE);
    }

    public boolean setSplashContentUrl(String splashContentUrl) {
        return putString(SPLASH_CONTENT_URL, splashContentUrl);
    }

    @Nullable
    public String getSplashContentUrl() {
        return getString(SPLASH_CONTENT_URL);
    }

    public boolean setBannerUrl(String bannerUrl) {
        return putString(BANNER_URL, bannerUrl);
    }

    @Nullable
    public String getBannerUrl() {
        return getString(BANNER_URL);
    }

    public boolean setIcon(String icon) {
        return putString(ICON, icon);
    }

    @Nullable
    public String getIcon() {
        return getString(ICON);
    }

    public boolean setBatAct(String batInfo) {
        return putString(BAT_ACT, batInfo);
    }

    @Nullable
    public String getBatAct() {
        return getString(BAT_ACT);
    }

}
